package com.park.platform.service.impl;

import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.AreaInfo;
import com.park.common.poo.ExpressInfo;
import com.park.common.util.AreaUtil;
import com.park.common.util.DateUtil;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IAreaService;
import com.park.platform.service.IExpressService;
import com.park.platform.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-04-30.
 */
@Service
public class ExpressServiceImpl extends BaseService implements IExpressService {

    @Autowired
    private IBaseDao baseDao;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IOperatorService operatorService;

    @Override
    public List<Map<String, Object>> getExpressList(int uid){

        List<Map<String, Object>> list = baseDao.queryBySql("SELECT * FROM express_info WHERE id = ? ORDER BY defaultFlag", uid);
        for(Map<String, Object> map : list){
            String areaId = StrUtil.objToStr(map.get("areaId"));
            AreaInfo areaInfo = areaService.getById(areaId);
            if(areaInfo != null){
                map.put("areaName", AreaUtil.getThumbnailAreaName(areaInfo.getAreaNameFull()) + map.get("address"));
            }
        }

        return list;
    }

    @Override
    public void newExpress(ExpressInfo expressInfo){
        if(operatorService.getUserOperator(expressInfo.getId()) == null) throw new MessageException("操作错误");
        if(StrUtil.isBlank(expressInfo.getName())) throw new MessageException("请输入收货人姓名");
        if(StrUtil.isBlank(expressInfo.getMobile())) throw new MessageException("请输入收货人电话");
        if(StrUtil.isBlank(expressInfo.getAreaId())) throw new MessageException("请选择收货城市");
        if(StrUtil.isBlank(expressInfo.getAddress())) throw new MessageException("请输入收货详细地址");

        clearDefaultExpress(expressInfo.getId());

        Integer expressId = expressInfo.getExpressId();
        String nowDate = DateUtil.getNowDate();
        if(expressId == null){
            expressInfo.setCreateTime(nowDate);
        }else{
            expressInfo.setUpdateTime(nowDate);
        }
        //if(!IDBConstant.LOGIC_STATUS_YES.equals(expressInfo.getDefaultFlag())) expressInfo.setDefaultFlag(IDBConstant.LOGIC_STATUS_NO);
        expressInfo.setDefaultFlag(IDBConstant.LOGIC_STATUS_YES);
        baseDao.save(expressInfo, expressId);
    }

    @Override
    public void clearDefaultExpress(int uid){
        List<ExpressInfo> expressInfos = baseDao.queryByHql("FROM ExpressInfo WHERE id = ? AND defaultFlag = ?", uid, IDBConstant.LOGIC_STATUS_YES);
        if(expressInfos != null && expressInfos.size() >0){
            for(ExpressInfo e : expressInfos){
                e.setDefaultFlag(IDBConstant.LOGIC_STATUS_NO);
                baseDao.save(e, e.getExpressId());
            }
        }
    }

    @Override
    public void newDeleteExpress(int uid, int expressId){
        ExpressInfo expressInfo = getExpressInfo(expressId);
        if(expressInfo.getId() != uid) throw new MessageException("操作错误");
        baseDao.delete(expressInfo);
        if(IDBConstant.LOGIC_STATUS_YES.equals(expressInfo.getDefaultFlag())){ //如果删除的是默认地址，则重新设置最新时间的为默认地址
            ExpressInfo e = baseDao.queryByHqlFirst("FROM ExpressInfo WHERE id = ? ORDER BY createTime DESC", uid);
            if(e != null) {
                e.setDefaultFlag(IDBConstant.LOGIC_STATUS_YES);
                baseDao.save(e, e.getExpressId());
            }
        }
    }

    @Override
    public void updateDefaultExpress(int uid, int expressId){
        clearDefaultExpress(uid);
        ExpressInfo expressInfo = getExpressInfo(expressId);
        if(expressInfo.getId() != uid) throw new MessageException("操作错误");
        expressInfo.setDefaultFlag(IDBConstant.LOGIC_STATUS_YES);
        baseDao.save(expressInfo, expressInfo.getExpressId());
    }

    @Override
    public ExpressInfo getExpressInfo(int expressId){
        return baseDao.getToEvict(ExpressInfo.class, expressId);
    }

}








